<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>yu</title>

<script>
var number_repeat=0;
	 $(function(){
	 	$.ajax({
	 		url:"/cn/cgi/api/item?item_code=cqssc",
	 		type:"GET",
	 		data:{},
	 		dataType:"json",
	 		success:function(msg){
	 			number_repeat = msg.number_repeat;
	 			view1(msg.layouts);
	 			$("#tabs").tabs();
	 			$("ul li a:first").click();
	 		}
	 	});
	 });

	 function view1(data){
	 	var sel = 0;
	 	for(var i=0;i<data.length;i++){
	 		var item = data[i];
	 		$("#ul").append('<li><a layout_code="'+item.layout_code+'" href="#'+item.layout_code+'" class="tab">'+item.name+'</a></li>');
	 		$("#ul").after('<div id="'+item.layout_code+'" class="tabcontent"></div>');

	 		if(item.children!=undefined){
	 			view2(item.children, item.layout_code);
	 		}
	 		if(item.selected==1){
	 			sel=i;
	 		}
	 	}


	 	$("#ul li a").unbind("click").bind("click", function(){
	 		var layout_code = $(this).attr("layout_code");
	 		$("#"+layout_code).find("input[type='radio']:first").click();
	 	});

	 	setTimeout(function(){
	 		$("#ul li:eq("+sel+") a").click();
	 	},10);
	 }

	 function view2(data, parent_id){
	 	for(var i=0;i<data.length;i++){
	 		var item = data[i];
	 		$("#"+parent_id).append('<div id="'+item.layout_code+'">'+item.name+'</div>');
	 		if(item.children!=undefined){
	 			view3(item.children, item.layout_code);
	 		}
	 	}
	 }

	 function view3(data, parent_id){
	 	for(var i=0;i<data.length;i++){
	 		var item = data[i];
	 		$("#"+parent_id).append('<input onchange="show_num(this)" type="radio" class="rule_id" name="rule_id" id="'+item.layout_code+'" data=\''+JSON.stringify(item)+'\' />');
	 		$("#"+parent_id).append('<label for="'+item.layout_code+'">'+item.name+'</label>');

	 	}
	 }

	 function show_num(v){
	 	// console.log("show_num");
	 	$(".count").html("0");
	 	var data = JSON.parse($(v).attr("data"));
	 	$(".nums").html("");

	 	var data = $(".rule_id:checked").attr("data");
	 	data = JSON.parse(data);
	 	var layout_code = data.layout_code;
	 	var need_row = data.need_row;
	 	var need_number = Number(data.need_number);
	 	var multiple = Number(data.multiple);
	 	$("#layout_code").html(layout_code);
	 	$("#need_row").html(need_row);
	 	$("#need_number").html(need_number);
	 	$("#multiple").html(multiple);

	 	var checkboxs = data.checkboxs;
	 	if(checkboxs!=undefined){
	 		var fun = data.input==1?"counts(event)":"counts(this)";
	 		for(var i=0;i<checkboxs.length;i++){
		 		var item = checkboxs[i];
		 		var content = item.label;
		 		var nums = "";
		 		for(var a=0;a<content.length;a++){
		 			// nums+="<a href='#'>"+content[a]+"</a>";
		 			var title = content[a];
		 			nums+="<input type='checkbox' onchange='"+fun+"' id='"+title+"' name='cbx' class='cbx' />";
		 			nums+="<label for='"+title+"'>"+title+"</label>";

		 		}
		 		$(".nums").append("<div><span>"+item.title+"</span>"+nums+"</div>");
		 	}
	 	}


	 	if(data.input==1){
	 		$(".nums").append("<textarea id='txt' onkeyup='counts(event)'></textarea>");
	 		return;
	 	}

	 	var selects = data.selects;
	 	if(selects==undefined){
	 		return;
	 	}

	 	for(var i=0;i<selects.length;i++){
	 		var item = selects[i];
	 		var content = item.content;
	 		var nums = "";
	 		for(var a=0;a<content.length;a++){
	 			nums+="<a href='#'>"+content[a]+"</a>";
	 		}
	 		$(".nums").append("<div><span>"+item.label+"</span>"+nums+"</div>");
	 	}

	 	//check rule_id
	 	$(".nums a").unbind("click").bind("click",function(){
	 		$(this).toggleClass("sel");
	 		counts(this);
	 	});

	 }

	 function counts2(event){
	 	var data = $(".rule_id:checked").attr("data");
	 	data = JSON.parse(data);
	 	var layout_code = data.layout_code;
	 	var need_row = data.need_row;
	 	var need_number = Number(data.need_number);
	 	var multiple = Number(data.multiple);

	 	var val = $('#txt').val();
	 	val = val.replace(/\s/ig,",");
	 	var counts = 0;

	 	console.log(val);
	 	var arr = val.split(",");
	 	var cbx = $(".cbx:checked").length;
	 	var cb = $(".cbx").length;
	 	// console.log(cbx);
	 	// for(var i=0;i<arr.length;i++){
	 	// 	var item = arr[i];
	 	// 	if(isNaN(item)){
	 	// 		continue;
	 	// 	}
	 	// 	if(item.length==need_row){
	 	// 		counts++;
	 	// 	}
	 	// }
	 	// if(cb>0){
	 	// 	var comb = number_combinations(cbx, need_row);
	 	// 	counts *= comb;
	 	// }


	 	$(".count").html(counts);

	 }

	 function counts(v){
	 	//count
	 	var data = $(".rule_id:checked").attr("data");
	 	data = JSON.parse(data);
		var rows = new Array();
		var select_num = new Array();
		cbx_leng = 0;
 		$(".nums div").each(function(i,n){
	 		var len = 0;
	 		$(n).find("a.sel").each(function(a,b){
	 			len++;
	 			select_num.push( i+""+$(b).html() );
	 		});
	 		if(len>0){
	 			rows.push(len);
	 		}
	 	});
	 	// var is_cb = $(".nums .cbx").length>0?1:0;
		var cbx_count = $(".nums .cbx:checked").length;
		if(data.input>0){
			var val = $('#txt').val();
		 	val = val.replace(/\s/ig,",");
		 	var counts = 0;
		 	select_num = val.split(",");
		 	var snum = new Array();
		 	for(var i=0;i<select_num.length;i++){
		 		if(isNaN(select_num[i])){
		 			continue;
		 		}
		 		snum.push(select_num[i]);
		 	}
		 	select_num = snum;
		}
		//data 配置数据 select_num选号 rows
		// console.log(data);
		// console.log(select_num);
		// console.log(rows);
		// console.log(cbx_count);
		var config = {
			need_row : data.need_row,
			need_number : data.need_number,
			multiple : data.multiple,
			input : data.input,
			notes : data.notes,
			checkboxs : data.checkboxs
		};
	 	var counts = count_note(config, number_repeat, select_num, rows, cbx_count);
	 	$(".count").html(counts);
	 }

	 function count_zuhe(t) {
  var n = 0;
  n = Object.keys(t).length;
    var u = !0
      , f = !1
      , d = void 0;
    try {
      for (var h, p = Object.keys(t)[Symbol.iterator](); !(u = (h = p.next()).done); u = !0) {
        var m = h.value;
        n *= t[m].length
      }
    } catch (e) {
      f = !0,
        d = e
    } finally {
      try {
        !u && p.return && p.return()
      } finally {
        if (f)
          throw d
      }
    }
    return n;
}

/**
 * 传入选号 计算注数
 * @param  {[data] 配置数据}  {"need_row": 2, "need_number": 1, "multiple": 1, "input": 1, "notes": {} , "checkboxs":{} }
 * @param  {[number_repeat] 选号是否重复} 1
 * @param  {[select_num] 选号} ["23", "36", "44"] // 行号+球号 23 第三[下标从0开始]行第3个号
 * @param  {[rows] 每行选号数} [2,1,1]
 * @param  {[cbx_count] checkbox 勾选数 } 3
 * @return {[int] 注数}
 */
function count_note(data, number_repeat, select_num, rows, cbx_count){
 	var layout_code = data.layout_code;
 	var need_row = data.need_row;
 	var need_number = Number(data.need_number);
 	var multiple = Number(data.multiple);
 	var input = Number(data.input);
 	var notes = data.notes;
 	var is_cb = data.checkboxs==undefined ?0:1; //是否有checkbox
 	var counts = 0;

    /**
     * 2017-10-07 传入layoutcode和is_special(1=特殊处理) 北京pk10 广东11x5
     * var config = '{need_row:"2",need_number:"1",multiple:"1",input:"1",notes:(   null),layout_code:"qianer_qianer_danshi",is_special:"1"}';
        var a = count_note(config,1,["11 21 56 09"],(null),"0");
     */
    var is_special = data.is_special;
    if (is_special=='1' && input>0 && layout_code.indexOf('danshi')>=0) {
        var note_count = select_num.length;
        if(typeof MyCount.count === "function"){
	 		MyCount.count(note_count);
	 	}
	 	return note_count;
    }

    //排列三 二码 前二码组选 复式
    if (layout_code=='erma_qianermazuxuan_fushi') {
        var count = select_num.length;
        var note_count = number_combinations(count,2);
        if(typeof MyCount.count === "function"){
	 		MyCount.count(note_count);
	 	}
        return note_count;
    }

    if (layout_code=='wuxing_wuxingzhixuan_zuhe' || layout_code=='housi_housizhixuan_zuhe' || layout_code=='qiansi_qiansizhixuan_zuhe') {
        var note_count = count_zuhe(select_num);
        if(typeof MyCount.count === "function"){
	 		MyCount.count(note_count);
	 	}
        return note_count;
    }

 	//手动输入
 	if(input>0){
	 	arr = select_num;
	 	for(var i=0;i<arr.length;i++){
	 		var item = arr[i];
	 		if(isNaN(item)){
	 			continue;
	 		}
	 		if(item.length==need_row){
	 			counts++;
	 		}
	 	}
	 	if(is_cb>0){
	 		var comb = number_combinations(cbx_count, need_row);
	 		counts *= comb;
	 	}
	 	
	 	if(typeof MyCount.count === "function"){
	 		MyCount.count(counts);
	 	}
	 	return counts;
 	}


 	//组选位置
 	if(is_cb>0){
		row = number_combinations(cbx_count,need_row);
		var num = select_num.length;
		num = number_combinations(num,need_number);
		counts = num*row*multiple;
		if(typeof MyCount.count === "function"){
	 		MyCount.count(counts);
	 	}
		return counts;
 	}


 	//和值
	var is_notes = 0;
	for(var key in notes){
		is_notes++;
	}
 	if(is_notes>0){
 		for(var a=0;a<select_num.length;a++){
 			for(var key in notes){
 				var val = select_num[a];
 				if(Number(key)==Number(val)){
 					counts+=Number(notes[key]);
 				}
 			}
 		}
 		if(typeof MyCount.count === "function"){
	 		MyCount.count(counts);
	 	}
 		return counts;
 	}


	combs = unique_combination(rows, need_row);
	//多行组合
 	for(var a=0;a<combs.length;a++){
 		var item = combs[a];
 		var item_count =0;
 		for(var b=0;b<item.length;b++){
 			//行内组合
 			var com = number_combinations(item[b], need_number)*multiple;
 			if(item_count==0){
 				item_count = com;
 				continue;
 			}
 			item_count*=com;
 		}
 		counts+=item_count;
 	}


 	//选号无重复
 	if(number_repeat==0){
		combs = unique_combination(select_num, need_row);
		for(var i=0;i<combs.length;i++){
			var item = combs[i];
			var all = new Array();
			for(var a=0;a<item.length;a++){
				if(all.indexOf(item[a][0])>=0){
					continue;
				}
				all.push(item[a][0]);
			}
			if(all.length<need_row){
				continue;
			}
			var arr = new Array();
			for(var a=0;a<item.length;a++){
			 	if(arr.indexOf(item[a][1]+""+item[a][2])<0){
					arr.push(item[a][1]+""+item[a][2]);
				}
				else{
					counts--;
					break;
				}
			}
		}
 	}

 	console.log(counts);

 	//调用android端的方法，返回counts
       if(typeof MyCount.count === "function"){
	 		MyCount.count(counts);
	 	}

 	return counts;
}



function factorial($n) {
    if ($n <= 1) {
        return 1;
    } else {
        return factorial($n - 1) * $n;
    }
}
//组合反回结果数不包括组合列表
function number_combinations_forLHC($n, $k, $y) {
    if ($n < $k) {
        MyCount.count(0, $y);
        return 0;
    } else {
        var number = factorial($n)/(factorial($k)*factorial(($n - $k)));
        MyCount.count(number, $y);
        return number;
    }
}

function number_combinations($n, $k) {
    if ($n < $k) {
        return 0;
    } else {
        return factorial($n)/(factorial($k)*factorial(($n - $k)));
    }
}
//组合反回组合列表
function unique_combination($arr, $sel){

	var aa = getCombination($arr,$sel);
	for(var i=0;i<aa.length;i++){
		var arr = (aa[i]+"").split(",");
		aa[i] = arr;
	}
	return aa;

    // $list = new Array();
    // $all = 0;
    // for ($i=0;$i< Math.pow(2, $arr.length);$i++){
    // 	$a = 0;
    //     $b = new Array();
    //     for ($j=0;$j<$arr.length;$j++){
    //         if ($i>>$j&1){
    //             $a++;
    //             $b.push($arr[$j]);
    //         }
    //     }
    //     if ($a==$sel){
    //         $list.push($b);
    //         $all++;
    //     }
    // }
    // return $list;
}
function getCombination(o, c) {
    var l = o.length;
    var r = new Array();
    var f = new Array();
    if (c > l) {
        return r
    }
    if (c == 1) {
        return o
    }
    if (l == c) {
        r[0] = o.join(",");
        return r
    }
    var a = "";
    var b = "";
    var s = "";
    for (var g = 0; g < c; g++) {
        a += "1";
        b += "1"
    }
    for (var e = 0; e < l - c; e++) {
        a += "0"
    }
    for (var d = 0; d < c; d++) {
        s += o[d] + ","
    }
    r[0] = s.substr(0, s.length - 1);
    var h = 1;
    s = "";
    while (a.substr(a.length - c, c) != b) {
        a = movestring(a);
        for (var d = 0; d < l; d++) {
            if (a.substr(d, 1) == "1") {
                s += o[d] + ","
            }
        }
        r[h] = s.substr(0, s.length - 1);
        s = "";
        h++
    }
    return r
}
function movestring(a) {
    var h = "";
    var k = "01";
    var b = "";
    var f = "";
    var j = "";
    var g = false;
    var c = false;
    for (var e = 0; e < a.length; e++) {
        if (g == false) {
            h += a.substr(e, 1)
        }
        if (g == false && a.substr(e, 1) == "1") {
            c = true
        } else {
            if (g == false && c == true && a.substr(e, 1) == "0") {
                g = true
            } else {
                if (g == true) {
                    b += a.substr(e, 1)
                }
            }
        }
    }
    h = h.substr(0, h.length - 2);
    for (var d = 0; d < h.length; d++) {
        if (h.substr(d, 1) == "1") {
            f += h.substr(d, 1)
        } else {
            if (h.substr(d, 1) == "0") {
                j += h.substr(d, 1)
            }
        }
    }
    h = f + j;
    return h + k + b
}

function get_random(min,max,number_format,data){
		min = Number(min+"");
		max = Number(max+"");
		var w = max - min;
		var r = Math.round(Math.random()*w+min);
		// console.log(r+","+w+","+max+","+min);
		if(data!=undefined){
			return data[r];
		}
		if(number_format==2){
			if(r<10){
				r = "0"+r;
			}
		}
		return r;
	}

	/**
	 * [generate_item 生成随机注 依赖函数 get_random ]
	 * @param  {[type]} config        [配置数据] {rule_id:143,need_row:1,need_number:1,input:0,selects:[{},{},{},{},{}],notes:{},multiple:1,max_odds:{0:"9.8"},checkboxs:null}
	 * @param  {[type]} number_repeat [从item接口获取]
	 * @param  {[type]} number_format [从item接口获取]
	 * @param  {[type]} number_range  [从item接口获取]
	 * @return {[type]}               [description]
	 */
	function generate_item(config,number_repeat,number_format,number_range){
		// console.log(JSON.stringify(config));
		number_repeat = Number(number_repeat+"");
		var rs = { rule_id:config.rule_id, pick_number: "", position:"" };
		var need_row = Number(config.need_row+"");
		var need_number = Number(config.need_number+"");
		var input = Number(config.input+"");
		console.log("fanwei " + number_range);
		var number_ranges = (number_range+"").split("-");
		console.log("number_ranges " + number_ranges);
		var selects = config.selects;
		var notes = config.notes;
		rs.amount_per_note = 2;
		rs.note_count = config.multiple;
		rs.rakeback=0;


		//checkbox
		if(config.checkboxs!=undefined){
			var arr = ["万位","千位","百位","十位","个位"];
			var pn = "";
			var all = ",";
			for(var i=0;i<need_row;i++){
				var r = get_random(0,4,1);
				while(all.indexOf(","+r+",")>=0){
					r = get_random(0,4,1);
				}
				all+=r+",";
				pn+=pn==""?"":",";
				pn+=arr[r];
			}
			rs.position = pn;
		}

		//手动
		if(input>0){
			var all = ",";
			for(var i=0;i<need_row;i++){
				var r = get_random(number_ranges[0],number_ranges[1],number_format);
				while(all.indexOf(","+r+",")>=0){
					r = get_random(number_ranges[0],number_ranges[1],number_format);
				}
				all+=r+",";
				if(number_format==2){
					rs.pick_number+=rs.pick_number==""?"":" ";
				}
				rs.pick_number+=r;
			}

			console.log("input " + rs.pick_number + " " + number_ranges[0] +  " " + number_ranges[1]);
			var contains = rs.pick_number.indexOf(",");
            var checkboxNum = 0;
            if(config.checkboxs!=undefined){
	            if(contains == -1){
	            	checkboxNum = rs.pick_number.length;
	            } else {
	            	var shuzuNum = rs.pick_number.split(",");
	            	checkboxNum = shuzuNum.length;
	            }
            }
            var shuzu2 = rs.pick_number.split("|");
            console.log("fff" + shuzu2);
            var shuzu3 = new Array();
            var shuzu4 = new Array();
            var hang = 0;
            for (var val in shuzu2) {
                console.log("jinlai");
    			if(input > 0){//手写
					if(shuzu2[val] != ""){
						shuzu4.push(shuzu2[val]);
					}
    			} else {
    				if(shuzu2[val] != ""){
    					var fen = shuzu2[val].split(",");
    					shuzu3.push(fen.length);
    					console.log("ddd" + shuzu2[val]);
    					for (var i = 0; i < fen.length; i++) {
    						// shuzu4.push(hang + "" + (i + 1));
    						shuzu4.push(hang + "" + selects[hang].content.indexOf(fen[i]));
    					}
    				}
    				hang++;
    			}
			}

			console.log(config);
			console.log(number_repeat);
			console.log(shuzu4);
			console.log(shuzu3);
			console.log(checkboxNum);
			var all_contents = count_note(config, number_repeat, shuzu4, shuzu3, checkboxNum);
			MyCount.getRandomString(rs.pick_number, all_contents);
			return [rs];
		}

		var min = number_ranges[0];
		var max = number_ranges[1];

		var data = undefined;
		console.log(selects);
		var selects = eval('(' + selects + ')');
		if(selects.length>0){
			data = selects[0].content;
			max = data.length-1;
			console.log(max);
		}

		//多行
		var pn = "";
		var start = get_random(0,selects.length-need_row,1);
		var all = ",";
		for(var i=0;i<selects.length;i++){
			var num = "";
			if(i>=start && i<start+need_row){
				for(l=0;l<need_number;l++){
					var r = get_random(0,max,number_format,data);
					while(all.indexOf(","+r+",")>=0){
						r = get_random(0,max,number_format,data);
					}
					all+=r+",";
					num+=num==""?"":",";
					num+=r;
				}
			}
			pn+=num;
			pn+="|";
		}
		pn = pn.substr(0,pn.length-1);
		rs.pick_number=pn;
		if(config.item_code == 'ahk3' && config.layout_code=='ertonghao_ertonghaodanxuan_biaozhunxuanhao'){
		    rs.pick_number = pn.substring(0, pn.length-1);
		}
		console.log("MMMMM " + rs.pick_number);

/**
             * 传入选号 计算注数
             * @param  {[data] 配置数据}  {"need_row": 2, "need_number": 1, "multiple": 1, "input": 1, "notes": {} , "checkboxs":{} }
             * @param  {[number_repeat] 选号是否重复} 1
             * @param  {[select_num] 选号} ["23", "36", "44"] // 行号+球号 23 第三[下标从0开始]行第3个号
             * @param  {[rows] 每行选号数} [2,1,1]
             * @param  {[cbx_count] checkbox 勾选数 } 3
             * @return {[int] 注数}
             function count_note(data, number_repeat, select_num, rows, cbx_count){
             */
		
            var contains = rs.pick_number.indexOf(",");
            var checkboxNum = 0;
            if(config.checkboxs!=undefined){
	            if(contains == -1){
	            	checkboxNum = rs.pick_number.length;
	            } else {
	            	var shuzuNum = rs.pick_number.split(",");
	            	checkboxNum = shuzuNum.length;
	            }
            }
            var shuzu2 = rs.pick_number.split("|");
            console.log("fff" + shuzu2);
            var shuzu3 = new Array();
            var shuzu4 = new Array();
            var hang = 0;
            for (var val in shuzu2) {
                console.log("jinlai");
    			if(input > 0){//手写
					if(shuzu2[val] != ""){
						shuzu4.push(shuzu2[val]);
					}
    			} else {
    				if(shuzu2[val] != ""){
    					var fen = shuzu2[val].split(",");
    					shuzu3.push(fen.length);
    					console.log("ddd" + shuzu2[val]);
    					for (var i = 0; i < fen.length; i++) {
    						// shuzu4.push(hang + "" + (i + 1));
    						shuzu4.push(hang + "" + selects[hang].content.indexOf(fen[i]));
    					}
    				}
    				hang++;
    			}
			}

			console.log("START");
            var description = "";
            for(var i in config){
                var property=config[i];
                description+=i+" = "+property+" ";
            }
            console.log(description);
 			console.log(number_repeat);
 			console.log(shuzu4);
			console.log(shuzu3);
 			console.log(checkboxNum);

            var all_contents;
 			if(config.item_code == 'bjssc' && (config.layout_code=='wuxing_wuxingzhixuan_zuhe' || config.layout_code=='housi_housizhixuan_zuhe' || config.layout_code=='qiansi_qiansizhixuan_zuhe')){
                var select_obj = {};
                select_obj['万位'] = new Array();
                select_obj['千位'] = new Array();
                select_obj['百位'] = new Array();
                select_obj['十位'] = new Array();
                select_obj['个位'] = new Array();

                for(var i=0; i<shuzu4.length; i++){
                	var str = shuzu4[i];
                	if(str.charAt(0) == '0'){
                		select_obj['万位'].push(str.charAt(1) + "");
                	} else if(str.charAt(0) == '1'){
                		select_obj['千位'].push(str.charAt(1) + "");
                	} else if(str.charAt(0) == '2'){
                		select_obj['百位'].push(str.charAt(1) + "");
                	} else if(str.charAt(0) == '3'){
                		select_obj['十位'].push(str.charAt(1) + "");
                	} else if(str.charAt(0) == '4'){
                		select_obj['个位'].push(str.charAt(1) + "");
                	}
                }

                if (select_obj['万位'].length == 0){
                	delete select_obj['万位'];
                } else if(select_obj['千位'].length == 0){
					delete select_obj['千位'];
                } else if(select_obj['百位'].length == 0){
                	delete select_obj['百位'];
                } else if(select_obj['十位'].length == 0){
                	delete select_obj['十位'];
                } else if(select_obj['个位'].length == 0){
                	delete select_obj['个位'];
                }

                console.log(JSON.stringify(select_obj));
                
                all_contents = count_note(config, number_repeat, select_obj, shuzu3, checkboxNum);
 			} else {
 			    all_contents = count_note(config, number_repeat, shuzu4, shuzu3, checkboxNum);
 			}
			MyCount.getRandomString(rs.pick_number, all_contents);
		return [rs];
	}
</script>

</head>

</html>
